﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Homework4
{
    public partial class MainWindow : Form
    {
        private const string Query1Desc = "Generate a file containing the Names of faculty members and their office numbers";
        private const string Query2Desc = "Generate an HTML webpage containing the names of faculty members (bold) and their office numbers (italics)";
        private const string Query3Desc = "Find all the associate professors who got their PHD degree after 1990. Output their names and research interests and email addresses.";
        private const string Query4Desc = "Find all the professors who are interested in artificial intelligence or databases, and output the name, title, research interests and courses offered.";
        private const string Query5Desc = "Find all courses which have a prerequisite of CSE 310";
        private const string Query6Desc = "Find the faculty members who have got their Ph.D from University of Maryland and who teach Database Management course. Output the faculty's name, office location, office hours, phone no.";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            DirectoryTextBox.Text = Environment.CurrentDirectory;

            var settingsWindow = new SettingsWindow();
            if (settingsWindow.ShowDialog() == DialogResult.OK)
            {
                DB.CreateDatabase(DB.DatabaseName);
                DB.CreateTables();
                DB.SyncSchemaToDatabase();

                Query1RadioButton.Checked = true;
            }
            else
            {
                Close();
            }
        }

        private void BrowseDirButton_Click(object sender, EventArgs e)
        {
            var folderBrowser = new FolderBrowserDialog { SelectedPath = DirectoryTextBox.Text };
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                DirectoryTextBox.Text = folderBrowser.SelectedPath;
            }
        }

        private void ParseButton_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(DirectoryTextBox.Text))
            {
                DataParser.ParseFiles(DirectoryTextBox.Text);
                MessageBox.Show("Parse finished! Instructors.xml should now exist in the given directory.\r\n\r\nIf it does not, then one or more of the text files was missing.", "Finished",
                                MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("Invalid directory given!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string _xQueryToRun;
        private string _sqlQueryToRun;

        private void QueryRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            var radioButton = ((RadioButton) sender);
            if (radioButton.Checked == false)
            {
                return;
            }

            var queryNum = int.Parse(radioButton.Text.Last().ToString());
            switch (queryNum)
            {
                case 1:
                    QueryLabel.Text = Query1Desc;
                    _xQueryToRun = XQuery.Query1;
                    _sqlQueryToRun = DB.Query1;
                    break;

                case 2:
                    QueryLabel.Text = Query2Desc;
                    _xQueryToRun = XQuery.Query2;
                    _sqlQueryToRun = DB.Query2;
                    break;

                case 3:
                    QueryLabel.Text = Query3Desc;
                    _xQueryToRun = XQuery.Query3;
                    _sqlQueryToRun = DB.Query3;
                    break;

                case 4:
                    QueryLabel.Text = Query4Desc;
                    _xQueryToRun = XQuery.Query4;
                    _sqlQueryToRun = DB.Query4;
                    break;

                case 5:
                    QueryLabel.Text = Query5Desc;
                    _xQueryToRun = XQuery.Query5;
                    _sqlQueryToRun = DB.Query5;
                    break;

                case 6:
                    QueryLabel.Text = Query6Desc;
                    _xQueryToRun = XQuery.Query6;
                    _sqlQueryToRun = DB.Query6;
                    break;
            }

            XqueryTextBox.Text = _xQueryToRun;
            SqlQueryTextBox.Text = _sqlQueryToRun;
        }

        private void QueryButton_Click(object sender, EventArgs e)
        {
            // Verify existence of the Instructors.xml file
            var xmlFilePath = Path.Combine(DirectoryTextBox.Text, "Instructors.xml");
            if (!File.Exists(xmlFilePath))
            {
                MessageBox.Show("Unable to find the Instructors.xml file in the given folder, please change the directory above to the location of the Instructors.xml file which is output from the parse action.",
                                "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            XqueryOutputWebBrowser.Url = null;
            SqlOutputGridView.DataSource = null;

            #region Run XQuery and Display Output in Web Browser Control

            XQuery.PerformQuery(_xQueryToRun, xmlFilePath,
                                Query2RadioButton.Checked ? XQuery.OutputType.Html : XQuery.OutputType.Xml, "Output");

            var outputFile = new FileInfo(Query2RadioButton.Checked ? "Output.html" : "Output.xml");
            if (outputFile.Exists)
            {
                XqueryOutputWebBrowser.Navigate(outputFile.FullName);
            }

            #endregion


            #region Run SQL Query and Display Table Output

            var table = DB.GetDataTableFromQuery(_sqlQueryToRun);
            if (table != null)
            {
                SqlOutputGridView.DataSource = table;
            }

            #endregion

        }
    }
}
